﻿// <copyright file="Point3DConverter.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    /// <summary>
    /// File name: Point3DConverter.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 09/30/2008 14:59:50 h.
    /// Description: Converts instances of other types to and from a Point3D structure
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    public sealed class Point3DConverter : TypeConverter
    {
        /// <summary>
        /// Indicate if the Point3D can be converted from the source type
        /// </summary>
        /// <param name="context">Contextual information</param>
        /// <param name="sourceType">source type to be converter to Point3D</param>
        /// <returns>bool indicanting if th conversion is possible</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
           if (sourceType.Equals(typeof(string)))
           {
               return true;
           }
           else if (sourceType.Equals(typeof(Point3D)))
           {
               return true;
           }
           else if (sourceType.Equals(typeof(Vector3D)))
           {
               return true;
           }
           return false;
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                return true;
            }
            else if (destinationType.Equals(typeof(Point3D)))
            {
                return true;
            }
            else if (destinationType.Equals(typeof(Vector3D)))
            {
                return true;
            }
            return false;
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value != null)
            {
                if (value is string)
                {
                    return Point3D.Parse((string)value);
                }
                else if (value is Point3D)
                {
                    return (Point3D)value;
                }
                else if (value is Vector3D)
                {
                    return (Point3D)value;
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                if (value != null)
                {
                    return ((Point3D)value).ToString();
                }
            }
            else if (destinationType.Equals(typeof(Vector3D)))
            {
                return ((Vector3D)value);
            }
            return null;
        }
        
    }
}
